#include <bits/stdc++.h>
using namespace std;
int n, m, l, a[505], c[505][505], f[505][505];
// c[i][j]表示将第i组的数全部改为模意义下为j的最小值
// f[i][j]表示前i组的数在模m意义下和为j的最小值
int main() {
  cin >> n >> m >> l;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i] > j)
        c[(i - 1) % l + 1][j] += j + m - a[i];
      else
        c[(i - 1) % l + 1][j] += j - a[i];
    }
  }
  memset(f, 0x3f, sizeof(f));
  f[0][0] = 0;
  // 跟E2.cpp 填表法相比，这是所谓的刷表法
  for (int i = 0; i < l; ++i) {
    for (int j = 0; j < m; ++j) {
      for (int k = 0; k < m; ++k) {
        f[i + 1][(j + k) % m] =
            min(f[i + 1][(j + k) % m], f[i][j] + c[i + 1][k]);
      }
    }
  }
  cout << f[l][0] << endl;
  return 0;
}